<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c_rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt"  prefix="fmt_rt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page language="java" 
	import="java.util.*" 
	import="com.jp.fm.data.*"
	import="com.jp.fm.*"
	pageEncoding="ISO-8859-1"%>

<%@page import="net.sf.ehcache.config.CacheConfiguration"%>
<%@page import="net.sf.ehcache.Element"%>
<%@page import="java.lang.reflect.Field"%>
<%@page import="java.lang.reflect.Method"%>
<%@page import="net.sf.ehcache.Statistics"%>
<%@page import="net.sf.ehcache.CacheManager"%>
<%@page import="net.sf.ehcache.Cache"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
	<head>
    	<base href="<%=basePath%>">
    
    	<title>HFM: Cache Manager</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
	
		<link rel="stylesheet" href="jquery-ui-1.10.0.custom/css/cupertino/jquery-ui-1.10.0.custom.css" />
		<script src="jquery-ui-1.10.0.custom/js/jquery-1.9.0.js"></script>
  		<script src="jquery-ui-1.10.0.custom/js/jquery-ui-1.10.0.custom.js"></script>
 
  		<link rel="stylesheet" href="css/fm.css" /> 
  		  		   
	</head>
	
<%-- Builds the caches to be listed in the page --%>
<%
Map cacheMap = new HashMap();
//List allCacheManagers = CacheManager.ALL_CACHE_MANAGERS;
//for (Iterator allCachesIter = allCacheManagers.iterator(); allCachesIter.hasNext();) {
//        CacheManager cacheManager = (CacheManager) allCachesIter.next();

		CacheManager cacheManager = HomeFinanceRunner.getCacheManager();
        String[] cacheNames = cacheManager.getCacheNames();
        for (int i=0; i < cacheNames.length; ++i) {
                Cache cache = cacheManager.getCache(cacheNames[i]);
                cacheMap.put(cache.getName(), cache);
        }
//}
%>
<%-- Handles the cacheAction=clear condition --%>
<%
if ("clear".equals(request.getParameter("cacheAction"))) {
        String cacheName = request.getParameter("cacheName");
        if (cacheName != null) {
                Cache cache = (Cache) cacheMap.get(cacheName);

                if (cache != null) {
                        cache.removeAll();
                }
        }
}
%>	
	
	<body>
  		<div id="page" class="page">
	    	<%@ include file="Header.jsp" %> 
	    	<%--<jsp:include page='Header.jsp' flush="true" />--%>
			<div id="main" class="main">		
			
<c:choose>
        <c:when test="${param.cacheAction == 'showStats'}">&nbsp;
                <%
                        String cacheName = request.getParameter("cacheName");
                        if (cacheName != null) {
                                Cache cache = (Cache) cacheMap.get(cacheName);

                                if (cache != null) {
                %>
                        <h1>Details for Cache Name '<%= cache.getName() %>' - <a class="click-link" url="/fm/secured/Cache.jsp">Return</a></h1>
                        <strong>In memory Size:</strong>&nbsp;<%= cache.calculateInMemorySize() %> Bytes<br/>
                        <strong>Objects In Cache:</strong>&nbsp;<%= cache.getSize() %><br/>
                        <strong>Memory Store Size:</strong>&nbsp;<%= cache.getMemoryStoreSize() %><br/>
                        <strong>Disk Store Size: </strong>&nbsp;<%= cache.getDiskStoreSize() %><br/>
                        <br/>
                        <h2>Cache Configuration</h2>
                        <%
                                CacheConfiguration cacheConfig = cache.getCacheConfiguration();
                                pageContext.setAttribute("cacheConfig", cacheConfig);
                        %>
                        <p>
                        <strong>Memory Store Eviction Policy:</strong>&nbsp;<%= cacheConfig.getMemoryStoreEvictionPolicy().toString() %><br/>
                        <strong>Cache Entry Eternal:</strong>&nbsp;<%= cacheConfig.isEternal() %><br/>
                        <%      if (!cacheConfig.isEternal()) { %>
                        <strong>Time To Live:</strong>&nbsp;<%= cacheConfig.getTimeToLiveSeconds() %> Seconds<br/>
                        <strong>Time To Sit Idle:</strong>&nbsp;<%= cacheConfig.getTimeToIdleSeconds() %> Seconds<br/>
                        <%      } %>
                        <strong>Max Items In Memory:</strong>&nbsp;<%= cacheConfig.getMaxElementsInMemory() %><br/>
                        <strong>Overflow To Disk:</strong>&nbsp;<%= cacheConfig.isOverflowToDisk() %><br/>
                        <%      if (cacheConfig.isOverflowToDisk()) { %>
                        <strong>Max Items On Disk:</strong>&nbsp;<%= cacheConfig.getMaxElementsOnDisk() %><br/>
                        <%      } %>
                        </p>
                        <h2>Statistics</h2>
                        <p>
                        <%
                                Statistics stats = cache.getStatistics();
                        %>
                        <strong>Total Cache Hits:</strong>&nbsp;<%= stats.getCacheHits() %><br/>
                        <strong>Total Cache Misses:</strong>&nbsp;<%= stats.getCacheMisses() %><br/>
                        <strong>Eviction Count:</strong>&nbsp;<%= stats.getEvictionCount() %><br/>
                        <strong>Memory Hit Count:</strong>&nbsp;<%= stats.getInMemoryHits() %><br/>
                        <%      if (cacheConfig.isOverflowToDisk()) { %>
                        <strong>Disk Hit Count:</strong>&nbsp;<%= stats.getOnDiskHits() %><br/>
                        <%      } %>
                        <strong>Average Get Time:</strong>&nbsp;<%= stats.getAverageGetTime() %><br/>
                        <strong>Stat Accuracy Mode:</strong>&nbsp;<%= stats.getStatisticsAccuracyDescription() %><br/>
                        </p>
                        <h2>Keys in Cache</h2>
                        <strong>Current Time: </strong>&nbsp;<%= new Date(System.currentTimeMillis()) %>
                        <table border="1">
                        <tr>
                        <th>Key</th>
                        <th>Expired</th>
                        <th>Hit Count</th>
                        <th>Creation</th>
                        <th>Expiration</th>
                        </tr>
                        <%
                                List cacheKeys = cache.getKeys();
                                for (Iterator cacheKeyIter = cacheKeys.iterator(); cacheKeyIter.hasNext();) {
                                        Object key = cacheKeyIter.next();
                                        pageContext.setAttribute("cacheName", cache.getName());
                                        pageContext.setAttribute("cacheValueKey", key.toString());
                                        Element cacheElement = cache.get(key);
                        %>
                        <tr>
                        <td><%= key.toString() %></td>
                        <% if (cacheElement != null) { %>
                        <td><%= cacheElement.isExpired() %></td>
                        <td><%= cacheElement.getHitCount() %></td>
                        <td><%= new Date(cacheElement.getCreationTime()) %></td>
                        <td><%= new Date(cacheElement.getExpirationTime()) %></td>
                        <% } else { %>
                        <td colspan="4">Object expired (waiting to be cleaned).</td>
                        <% } %>
                        </tr>
                        <%
                                }
                        %>
                        </table>
                <%
                                }
                        } else {
                %>
                ERROR: Cache Not found!
                <%
                        }
                %>
        </c:when>
        <c:otherwise>
                <h1>Welcome to the Cache Manager</h1>
                <table border="1">
                <tr>
                <th>Cache Name</th>
                <th>Entry Count</th>
                <th>Memory Size (Bytes)</th>
                <th colspan="2">Actions</th>
                </tr>
                <%
                long totalMemorySize = 0;
                for (Iterator cacheIter = cacheMap.values().iterator(); cacheIter.hasNext();) {
                        Cache cache = (Cache) cacheIter.next();
                        long entryMemorySize = cache.calculateInMemorySize();
                        totalMemorySize += entryMemorySize;
                %>
                <tr>
                <td><%= cache.getName() %></td>
                <td align="right"><%= cache.getSize() %></td>
                <td align="right"><%= entryMemorySize %></td>
                <td><a class="click-link" url="/fm/secured/Cache.jsp?cacheName=<%= cache.getName()%>&cacheAction=showStats">Show Stats</a></td>
                <td><a class="click-link" url="/fm/secured/Cache.jsp?cacheName=<%= cache.getName()%>&cacheAction=clear">Clear</a></td>
                </tr>
                <%
                }
                %>
                </table>
                <strong>Total Cache Memory Size:</strong>&nbsp;<%= totalMemorySize %> Bytes<br/>
        </c:otherwise>
</c:choose>				
			
			</div>
			<%@ include file="Footer.jsp" %> 
			<%--<jsp:include page='Footer.jsp' flush="true" />--%>	    
		</div>				
	</body>
</html>